/*
* UserController.java
* Created on  2013-9-26 下午10:42
* 版本       修改时间          作者      修改内容
* V1.0.1    2013-9-26       yutong    初始版本
*
*/
package com.genghis.ptas.consumer;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.postreply.entity.PostReply;
import com.genghis.ptas.postreply.service.PostsReplyService;
import com.genghis.ptas.posts.entity.Posts;
import com.genghis.ptas.posts.service.PostsService;
import com.genghis.ptas.scorm.entity.ScormInfo;
import com.genghis.ptas.scorm.service.UpScormService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.userfriends.entity.UserFriend;
import com.genghis.ptas.userfriends.service.UserFriendService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.List;

/**
 * @author yutong
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/consumer/")
public class ConsumerController {

    @Autowired
    private UserService userService;

    @Autowired
    private UpScormService upScormService;

    @Autowired
    private PostsReplyService postsReplyService;

    @Autowired
    private PostsService postsService;

    @Autowired
    private UserFriendService userFriendService;

    @RequestMapping(value = "upFileDo", method = {RequestMethod.GET})
    public String upFileDo() {
        return "consumer/upFile";
    }
    @RequestMapping(value = "returnFirstPage", method = {RequestMethod.GET})
    public String returnFirstPage(HttpServletRequest request){
        List<ScormInfo> scormScoreList = upScormService.listAllScormByAvgScore();
        List<ScormInfo> scormRegistNumList = upScormService.listAllScormByRegistNum();
        List<ScormInfo> scormLoadTimeList = upScormService.listAllScormByUploadTime();
        request.setAttribute("scormLoadTimeList",scormLoadTimeList);
        request.setAttribute("scormRegistNumList",scormRegistNumList);
        request.setAttribute("scormScoreList",scormScoreList);
        return "consumer/usermain";
    }
    @RequestMapping(value = "registerDo", method = {RequestMethod.GET})
    public String register(HttpServletRequest request) {
        return "tourist/register";
    }
    @RequestMapping(value = "addUser", method = {RequestMethod.POST})
    @ResponseBody
    public void addUser(User user) {
        userService.addUser(user);
    }
    @RequestMapping(value = "listAllPostsDo", method = {RequestMethod.GET})
    public String listAllPostsDo() {
        return "consumer/listAllPosts";
    }
    @RequestMapping(value = "listAllPostsManage", method = {RequestMethod.GET})
    public String listAllPostsManage() {
        return "admin/posts/listAllPostsManage";
    }
    @RequestMapping(value = "addPosts",method = {RequestMethod.POST})
    @ResponseBody
    public void addPosts(Posts posts){
        int userId=userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        posts.setPostPersonId(userId);
        postsService.addPosts(posts);
    }
    @RequestMapping(value = "addPostReply",method = {RequestMethod.POST})
    @ResponseBody
    public void addPostReply(PostReply postReply){
        int userId=userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        postReply.setReplyPersonId(userId);
        postsReplyService.addPostReply(postReply);
    }

    @RequestMapping(value = "listAllPosts",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Page<Posts> listAllPosts(PageParameter pageParameter,Posts posts){
         return  postsService.findPostsPageList(pageParameter, posts);
    }
    @RequestMapping(value = "findOnePost",method = {RequestMethod.POST,RequestMethod.GET})
    public String findOnePost(PageParameter pageParameter,PostReply postReply,HttpServletRequest request,@RequestParam("postId") int postId){
       List<PostReply> postsList= postsReplyService.findPostReplyPageList(pageParameter, postReply, postId);
       Posts posts= postsService.findPostById(postId);
       request.setAttribute("posts",posts);
       request.setAttribute("postsList",postsList);
       return  "consumer/listAllReply";
    }
    @RequestMapping(value = "delPosts", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delPosts(@RequestParam("ids") String ids) throws GeneralSecurityException {
        String[] postsIds = ids.split(",");
        postsService.delPosts(postsIds);
    }
    @RequestMapping(value = "findOnePostManage",method = {RequestMethod.POST,RequestMethod.GET})
    public String findOnePostManage(PageParameter pageParameter,PostReply postReply,HttpServletRequest request,@RequestParam("postId") int postId){
        List<PostReply> postsList= postsReplyService.findPostReplyPageList(pageParameter, postReply, postId);
        Posts posts= postsService.findPostById(postId);
        request.setAttribute("posts",posts);
        request.setAttribute("postsList",postsList);
        return  "admin/posts/listAllReplyManage";
    }
    @RequestMapping(value = "delReply", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delReply(@RequestParam("id") String id) throws GeneralSecurityException {
        postsReplyService.delOneReplyById(Integer.parseInt(id));
    }
    @RequestMapping(value = "listAllFriendDo", method = {RequestMethod.GET})
    public String listAllFriendDo() {
        return "consumer/listAllFriends";
    }
    @RequestMapping(value = "addFriendDo", method = {RequestMethod.GET})
    public String addFriendDo(HttpServletRequest request) {
        int userId=userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        List<String> friendList=  userFriendService.findFriendId(userId);
        request.setAttribute("friendList",friendList);
        return "consumer/findFriend";
    }
    @RequestMapping(value = "findOneFriend",method = {RequestMethod.POST})
    @ResponseBody
    public Page findOneFriend(PageParameter pageParameter,User user){

     return userService.findUserPageList(pageParameter, user);
    }
    @RequestMapping(value = "findAllFriend",method = {RequestMethod.POST})
    @ResponseBody
    public Page findAllFriend(PageParameter pageParameter,UserFriend userFriend){
        int userId=userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        userFriend.setUserId(userId);
        return userFriendService.findFriendPageList(pageParameter,userFriend);
    }
    @RequestMapping(value = "addFriend",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public void addFriend(UserFriend userFriend){
        int userId=userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        userFriend.setUserId(userId);
        userFriendService.addFriend(userFriend);
    }
    @RequestMapping(value = "delFriend", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delFriend(@RequestParam("id") String id) throws GeneralSecurityException {
        userFriendService.delFriendById(Integer.parseInt(id));
    }
    @RequestMapping(value = "friendCourseDo", method = {RequestMethod.GET})
    public String friendCourseDo() {
        return "consumer/listAllFriendCourse";
    }
    @RequestMapping(value = "friendCourse",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Page friendCourse(PageParameter pageParameter, ScormInfo scormInfo){
        int userId=userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return userFriendService.findFriendCoursePageList(pageParameter, scormInfo,userId);
    }
}
